package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebTagContentDao;
import ysq.xyg.web.model.po.WebTagContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebTagContentVo;
import ysq.xyg.web.service.WebTagContentService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-标签内容关联表service实现
 *
 * @author shengQiang yu
 * @date 2019-6-7 20:44:32
 */
@Service("webTagContentService")
public class WebTagContentServiceImpl extends BaseServiceImpl<WebTagContent> implements WebTagContentService {

    @Resource
    private WebTagContentDao webTagContentDao;

    private WebTagContent webTagContent;

    @Autowired
    public void setBaseDao(WebTagContentDao webTagContentDao) {
        this.baseDao = webTagContentDao;
    }


    /**
     * 分页查询
     *
     * @param webTagContentVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:32
     */
    public ReturnEntity page(WebTagContentVo webTagContentVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebTagContent> list = acceptList(webTagContentVo);
        PageInfo<WebTagContent> page=new PageInfo<WebTagContent>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webTagContentVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:32
     */
    public ReturnEntity addOrUpdate(WebTagContentVo webTagContentVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webTagContentVo.getId())){
            webTagContent=new WebTagContent(currentUser);
            isUpate=false;
        }else{
            webTagContent=selectByPrimaryKey(webTagContentVo.getId());
            if(ObjectUtil.isNull(webTagContent)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webTagContent.setUpdateTime(new Date());
            webTagContent.setUpdateUser(currentUser.getId());
        }
        webTagContent.setTagId(webTagContentVo.getTagId());
        webTagContent.setTagType(webTagContentVo.getTagType());
        webTagContent.setExtendId(webTagContentVo.getExtendId());
        webTagContent.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webTagContent);
        }else{
            num=insert(webTagContent);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:32
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebTagContent webTagContent=selectByPrimaryKey(id);
        if(webTagContent!=null){
            webTagContent.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webTagContent.setUpdateTime(new Date());
            webTagContent.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webTagContent);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webTagContentVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-7 20:44:32
     */
    public List<WebTagContent> acceptList(WebTagContentVo webTagContentVo){
        Example example = new Example(WebTagContent.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webTagContentVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webTagContentVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webTagContentVo.getUserId())){
            criteria.andEqualTo("userId",webTagContentVo.getUserId());
        }
        if(StringUtil.isNotEmpty(webTagContentVo.getExtendId())){
            criteria.andEqualTo("extendId",webTagContentVo.getExtendId());
        }
        if(StringUtil.isNotEmpty(webTagContentVo.getTagId())){
            criteria.andEqualTo("tagId",webTagContentVo.getTagId());
        }
        if(StringUtil.isNotEmpty(webTagContentVo.getTagType())){
            criteria.andEqualTo("tagType",webTagContentVo.getTagType());
        }
        example.setOrderByClause("update_time desc");
        return webTagContentDao.selectByExample(example);
    }
}
